﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 支持触摸按下的按钮
    /// </summary>
    public class TouchButton : ButtonBase
    {
        public TouchButton()
        {
        }
        /// <summary>
        /// 是否允许使用触摸点击
        /// </summary>
        public bool IsTouchEnabled
        {
            get { return (bool)GetValue(IsTouchEnabledProperty); }
            set { SetValue(IsTouchEnabledProperty, value); }
        }

        public static readonly DependencyProperty IsTouchEnabledProperty =
            DependencyProperty.Register("IsTouchEnabled", typeof(bool), typeof(TouchButton), new PropertyMetadata(false));




        private void SetIsPressed(bool pressed)
        {
            base.IsPressed = pressed;
        }
        protected override void OnTouchDown(TouchEventArgs e)
        {
            if (IsTouchEnabled)
            {
                if (IsPressed || !IsEnabled)
                {//已经按下，不再处理
                    return;
                }
                if (ClickMode != ClickMode.Hover)
                {
                    e.Handled = true;
                    this.CaptureTouch(e.TouchDevice);
                    SetIsPressed(true);
                    if (ClickMode == ClickMode.Press)
                    {
                        bool exceptionThrown = true;
                        try
                        {
                            OnClick();
                            exceptionThrown = false;
                        }
                        finally
                        {
                            if (exceptionThrown)
                            {
                                // Cleanup the buttonbase state
                                SetIsPressed(false);
                                ReleaseTouchCapture(e.TouchDevice);
                            }
                        }
                    }
                }
            }

            base.OnTouchDown(e);
        }
        protected override void OnTouchUp(TouchEventArgs e)
        {
            base.OnTouchUp(e);

            if (IsTouchEnabled && ClickMode != ClickMode.Hover)
            {
                e.Handled = true;
                bool shouldClick = IsPressed && ClickMode == ClickMode.Release;

                if (this.TouchesCaptured.Contains(e.TouchDevice))
                {
                    ReleaseTouchCapture(e.TouchDevice);
                }

                if (shouldClick)
                {
                    OnClick();
                }
            }

        }

        protected override void OnLostTouchCapture(TouchEventArgs e)
        {
            base.OnLostTouchCapture(e);
            if (IsTouchEnabled && (e.OriginalSource == this) && (ClickMode != ClickMode.Hover))
            {
                if (IsPressed)
                {//失去触点时取消按下状态
                    SetIsPressed(false);
                }
            }
        }


        protected override void OnTouchEnter(TouchEventArgs e)
        {
            base.OnTouchEnter(e);
            if (IsTouchEnabled && HandleIsTouchOverChanged(e.TouchDevice))
            {
                e.Handled = true;
            }
        }

        protected override void OnTouchLeave(TouchEventArgs e)
        {
            base.OnTouchLeave(e);
            if (IsTouchEnabled && HandleIsTouchOverChanged(e.TouchDevice))
            {
                e.Handled = true;
            }
        }

        private bool HandleIsTouchOverChanged(TouchDevice touchDevice)
        {
            if (ClickMode == ClickMode.Hover)
            {
                if (this.TouchesOver.Contains(touchDevice))
                {
                    // Hovering over the button will click in the OnHover click mode
                    SetIsPressed(true);
                    OnClick();
                }
                else
                {
                    SetIsPressed(false);
                }

                return true;
            }
            return false;
        }

        protected override void OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnIsPressedChanged(e);
            try
            {
                IsPressedChanged?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// 在按钮的“按下”状态改变时触发
        /// </summary>
        public event EventHandler IsPressedChanged;
    }
}
